<?php

namespace app\api\model\kuyou\applet;

use app\api\controller\kuyou\applet\Commons;
use think\Db;
use think\Model;
class CustomerGoodscoding extends Model
{

    // 表名
    protected $name = 'kuyou_customer_goodscoding';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getList($where,$page,$limit){
        $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
        $pageStatus = true;
        if($app_page){
            if($page<0){
                $pageStatus = false;
            }
        }
        if(isset($where['customers_id']) && !empty($where['customers_id'])){
            $customers = (new Customers())->where(['customer_code'=>$where['customers_id']])->field('id,customer_code')->find();
            if(empty($customers)){
                return ['status'=>500,'msg'=>'客户不存在'];
            }
            $where['customers_id'] = $customers['id'];
        }
        if(isset($where['goods_code']) && !empty($where['goods_code'])){
            $goods = (new Goods())->where(['goods_code'=>$where['goods_code']])->field('id,goods_code')->find();
            if(empty($goods)){
                return ['status'=>500,'msg'=>'商品不存在'];
            }
        }
        $map = [];
        foreach ($where as $where_k=>$where_v){
            $map['cg.'.$where_k] = $where_v;
        }
        if($pageStatus){
            $list = Db::name('kuyou_customer_goodscoding')->alias('cg')
                ->join('kuyou_goods g','g.goods_code=cg.goods_code')
                ->join('kuyou_customers c','c.id=cg.customers_id')
                ->join('kuyou_unit u','u.id=cg.unit_id')
                ->where($map)
                ->page($page,$limit)
                ->field('cg.id,cg.customer_code,FROM_UNIXTIME(cg.createtime,\'%Y-%m-%d %H:%i:%s\') as createtime,FROM_UNIXTIME(cg.updatetime,\'%Y-%m-%d %H:%i:%s\') as updatetime,cg.goods_code,g.goods_name,cg.customers_id,c.customer_name,cg.unit_id,u.name')
                ->select();
        }else{
            $list = Db::name('kuyou_customer_goodscoding')->alias('cg')
                ->join('kuyou_goods g','g.goods_code=cg.goods_code')
                ->join('kuyou_customers c','c.id=cg.customers_id')
                ->join('kuyou_unit u','u.id=cg.unit_id')
                ->where($map)
                ->field('cg.id,cg.customer_code,FROM_UNIXTIME(cg.createtime,\'%Y-%m-%d %H:%i:%s\') as createtime,FROM_UNIXTIME(cg.updatetime,\'%Y-%m-%d %H:%i:%s\') as updatetime,cg.goods_code,g.goods_name,cg.customers_id,c.customer_name,cg.unit_id,u.name')
                ->select();
        }
        $count = Db::name('kuyou_customer_goodscoding')->alias('cg')
            ->join('kuyou_goods g','g.goods_code=cg.goods_code')
            ->join('kuyou_customers c','c.id=cg.customers_id')
            ->join('kuyou_unit u','u.id=cg.unit_id')
            ->where($map)
            ->count();
        return ['list'=>$list,'count'=>$count];
    }

    /**
     * 获取详情
     * @param int $id
     * @return array
     */
    public static function getDetail($id){
        $detail = Db::name('kuyou_customer_goodscoding')->alias('cg')
            ->join('kuyou_goods g','g.goods_code=cg.goods_code')
            ->join('kuyou_customers c','c.id=cg.customers_id')
            ->join('kuyou_unit u','u.id=cg.unit_id')
            ->where(['cg.id'=>$id])
            ->field('cg.id,cg.customer_code,FROM_UNIXTIME(cg.createtime,\'%Y-%m-%d %H:%i:%s\') as createtime,FROM_UNIXTIME(cg.updatetime,\'%Y-%m-%d %H:%i:%s\') as updatetime,cg.goods_code,g.goods_name,cg.customers_id,c.customer_name,cg.unit_id,u.name')
            ->find();
        if($detail){
            return ['status'=>200,'msg'=>$detail];
        }else{
            return ['status'=>500,'msg'=>'未查询到相关信息'];
        }
    }

    /**
     * 添加
     * @param array $detail
     * @return array
     */
    public static function addDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'code_data|物料编码'=>'require',
                'customers_id|客户'=>'require|number',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $customers = (new Customers())->where(['id'=>$detail['customers_id']])->value('id');
                if(empty($customers)){
                    return ['status'=>500,'msg'=>'客户不存在'];
                }
                $code_data = (array)json_decode(html_entity_decode($detail['code_data']), true);
                foreach ($code_data as $code_data_k=>$code_data_v){
                    $goods = (new Goods())->where(['goods_code'=>$code_data_v['goods_code']])->value('id');
                    if(empty($goods)){
                        return ['status'=>500,'msg'=>'商品不存在'];
                    }
                    $unit = (new Unit())->where(['id'=>$code_data_v['unit_id']])->value('id');
                    if(empty($unit)){
                        return ['status'=>500,'msg'=>'单位不存在'];
                    }
                    $code_data[$code_data_k]['customers_id'] = $detail['customers_id'];
                    $code_data[$code_data_k]['createtime'] = time();
                    $code_data[$code_data_k]['updatetime'] = time();
                }
                $res = (new CustomerGoodscoding())->insertAll($code_data);
                if($res){
                    return ['status'=>200,'msg'=>sizeof($code_data)];
                }else{
                    return ['status'=>500,'msg'=>'添加失败'];
                }
            }
        }
    }

    /**
     * 更新
     * @param array $detail
     * @return array
     */
    public static function editDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'id|ID'=>'require|number',
                'customer_code|物料编码'=>'require',
                'customers_id|客户'=>'require|number',
                'goods_code|商品编号'=>'require',
                'unit_id|单位'=>'require|number',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $customers = (new Customers())->where(['id'=>$detail['customers_id']])->value('id');
                if(empty($customers)){
                    return ['status'=>500,'msg'=>'客户不存在'];
                }
                $goods = (new Goods())->where(['goods_code'=>$detail['goods_code']])->value('id');
                if(empty($goods)){
                    return ['status'=>500,'msg'=>'商品不存在'];
                }
                $unit = (new Unit())->where(['id'=>$detail['unit_id']])->value('id');
                if(empty($unit)){
                    return ['status'=>500,'msg'=>'单位不存在'];
                }
                $id = $detail['id'];unset($detail['id']);
                $detail['updatetime'] = time();
                $res = (new CustomerGoodscoding())->where(['id'=>$id])->update($detail);
                if($res){
                    return ['status'=>200,'msg'=>'更新成功'];
                }else{
                    return ['status'=>500,'msg'=>'更新失败'];
                }
            }
        }
    }

    /**
     * 删除
     * @param int $id
     * @return array
     */
    public static function delDetail($id){
        if(empty($id)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $res = (new CustomerGoodscoding())->where(['id'=>['in',$id]])->delete();
            if($res){
                return ['status'=>200,'msg'=>$res];
            }else{
                return ['status'=>500,'msg'=>'删除失败'];
            }
        }
    }

    /**
     * 导入
     * @param array $add,$edit
     * @return array
     */
    public static function import($add,$edit){
        if(empty($add) && empty($edit)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            if(!empty($add)){
                $res = (new CustomerGoodscoding())->insertAll($add);
            }
            if(!empty($edit)){
                foreach ($edit as $edit_k=>$edit_v){
                    $res = (new CustomerGoodscoding())->where(['id'=>$edit_v['id']])->update($edit_v);
                }
            }
            if($res){
                return ['status'=>200,'msg'=>'导入成功'];
            }else{
                return ['status'=>500,'msg'=>'导入失败'];
            }
        }
    }
}
